/*********************************************************
*Copyright (c) 2015 zhongsx. All Rights in Pw 
*@des: B型事件处理队列 
* 使用生产者多消费者模型
*@name: BMsgQueue.h
*@author: zhongsx
*@time: 2015-02-05
*********************************************************/

#ifndef __BMSG_QUEUE_H_
#define __BMSG_QUEUE_H_

#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/recursive_mutex.hpp>  
#include <boost/thread/condition_variable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/unordered_map.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/function.hpp>


// typedef boost::mutex                   Uni_Mutex;  
// typedef boost::shared_mutex            WR_Mutex;  
// typedef boost::unique_lock<WR_Mutex>   writeLock;  
// typedef boost::shared_lock<WR_Mutex>   readLock;  
// typedef boost::recursive_mutex         Rcs_Mutex;  
// typedef boost::unique_lock<Rcs_Mutex>  recuLock;  
#define MSGQUEUE_SIZE 	1000
typedef boost::function<void()> CallBackNode;
typedef std::list<CallBackNode> T_list;

class BMsgQueue
{
private:
	//
	mutable boost::mutex 				_mutex;
	//条件变量 当有数据时合并
	boost::condition_variable_any 		_variable;
	//执行事件链表
	boost::circular_buffer<CallBackNode> 	_queue; 
public:
	static boost::unordered_map<int, boost::shared_ptr<BMsgQueue> > QueueMap;
	static boost::mutex 	queue_mutex;	
	//单例管理队列
	static boost::shared_ptr<BMsgQueue> GetMsgQueue(int _type);
	static void DestroyInstance(int _type);

public:
	//typedef boost::circular_buffer<CallBackNode> T_list;

	BMsgQueue();
	virtual ~BMsgQueue();
	//
	bool empty() const ;

	int size() const ;

	int push_wait_queue(boost::shared_ptr<BMsgQueue> msgq, int size);

	void  push_list(T_list& list);

	//push 1 个
	void push_back(CallBackNode node);

	CallBackNode get_front();
	
	T_list get_node_list(int size);
};

#endif //__BMSG_QUEUE_H_